package com.example.administrator.myproject.login

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.example.administrator.myproject.App
import com.example.administrator.myproject.MainActivity
import com.example.administrator.myproject.R
import com.example.administrator.myproject.bean.User
import kotlinx.android.synthetic.main.login.*

class login22 : AppCompatActivity(), View.OnClickListener {
    private val TAG = "Login"
    var Userid: EditText? = null
    var Password: EditText? = null
//    var application: App = this.application as App
    var i = intent

    override fun onCreate(savedInstanceState: Bundle?) {
        var application: App = this.application as App
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login)
        tv_go_to_register.setOnClickListener{
            var Intent = Intent(this, Register22::class.java)
            startActivity(Intent)
        }
        Userid = findViewById(R.id.et_user_account)
        Password = findViewById(R.id.et_user_password)
        findViewById<View>(R.id.btn_login).setOnClickListener(this)


    }
    override fun onClick(view: View) {

        when (view.id) {
            R.id.btn_login -> {
                i = Intent(this, MainActivity::class.java)
                var application: App = this.application as App
                val userid = Userid!!.text.toString()
                val password = Password!!.text.toString()
                val query: BmobQuery<User> = BmobQuery<User>()
                if (userid.isEmpty()){
                    Toast.makeText(this, " 请输入账户", Toast.LENGTH_SHORT).show()
                }
                else if(password.isEmpty()) {
                    Toast.makeText(this, " 请输入密码", Toast.LENGTH_SHORT).show()
                }
                else {
                    query.addWhereEqualTo("userid", userid)
                    query.findObjects(object : FindListener<User>() {
                        override fun done(list: List<User>, e: BmobException?) {
                            if (e == null) {
                                if(list[0].userid == userid && list[0].password == password && list[0].status == "2") {
                                    Log.i(Companion.TAG, "done: " + list.toString())
                                    startActivity(i)
                                    application.setUserid(list[0].userid)
                                    application.setPassword(list[0].password)
                                    application.setMoney(list[0].money)
                                    Toast.makeText(this@login22, "登录成功", Toast.LENGTH_SHORT).show()
                                }
                            } else {

                            }
                        }
                    })
                }
            }
        }
    }
    companion object {
        private const val TAG = "Login"
    }
}
